package com.oa.modules.approvalsec1.controller;

import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;
import com.oa.modules.approvalsec1.entity.LicenceEntity;
import com.oa.modules.approvalsec1.service.LicenceService;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("licence")
@Api(tags = "证照变更注销材料查询相关接口")
public class LicenceController extends AbstractController  {

    @Autowired
    private LicenceService licenceService;

    private static final Logger log = LoggerFactory.getLogger(LicenceController.class);

    @GetMapping("/getFirst")
    @ApiOperation(value = "一级分类")
    public R getFirst() {

        List<LicenceEntity> list = licenceService.getFirst();
        return R.ok().put("data", list);
    }

    @GetMapping("/getChildrenById/{id}")
    @ApiOperation(value = "通过ID获取子类")
    public R getChildrenById(@PathVariable("id") Integer id) {
        log.info("id-----" + id);
        List<LicenceEntity> list = licenceService.getChildrenById(id);
        return R.ok().put("data", list);
    }

    @PostMapping("/getTextByIds")
    @ApiOperation(value = "通过ID数组获取材料要求和法定依据")
    public R getTextByIds(@RequestBody Integer[] ids) {
        if(ids.length>0){
            log.info("ids-----" + Arrays.toString(ids));
            List<LicenceEntity> list = licenceService.getTextByIds(ids);
            return R.ok().put("data", list);
        }else {
            return R.ok().put("data", null);
        }

    }


    @GetMapping("/getTree")
    @ApiOperation(value = "获取所有类别")
    public R getTree() {

        List<LicenceEntity> list = licenceService.getTree();

        return R.ok().put("data", list);
    }

    @PostMapping("/getChildrenByName")
    @ApiOperation(value = "通过名称获取具体变更子类（父类查询）")
    public R getChildrenByName(@RequestBody String name) {

        log.info("name-----" + name);
        List<LicenceEntity> list = licenceService.getChildrenByName(name);
        return R.ok().put("data", list);


    }


    @PostMapping("/getChildrenByName2")
    @ApiOperation(value = "通过名称获取具体变更子类（父子联查）")
    public R getChildrenByName2(@RequestBody String name) {

        log.info("name-----" + name);
        List<LicenceEntity> list = licenceService.getChildrenByName2(name);
        return R.ok().put("data", list);


    }

    @PostMapping("/submit")
    @ApiOperation(value = "提交")
    public R submit(@RequestBody Map param) {
        log.info("param-----" + param);
        String ids = (String) param.get("ids");
        String username = (String) param.get("username");
        String idcard = (String) param.get("idcard");
        String phone = (String) param.get("phone");
        String[] idarray = ids.split(",");
        if(idarray.length>0){
            for(int i=0;i<idarray.length;i++){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("id",idarray[i]);
                map.put("username",username);
                map.put("idcard",idcard);
                map.put("phone",phone);
                licenceService.submit(map);
            }
            //查询文件地址
            List<String> list = licenceService.selectFile(idarray);
            return R.ok().put("data", list);
        }else {
            return R.error("空");
        }
    }


    @GetMapping("/getCategoryTotal")
    @ApiOperation(value = "按分类统计")
    public R getCategoryTotal() {
        List<Map> list = licenceService.getCategoryTotal();
        return R.ok().put("data", list);
    }


    @GetMapping("/getDateTotal")
    @ApiOperation(value = "按日期统计")
    public R getDateTotal() {

        List<Map> list = licenceService.getDateTotal();

        return R.ok().put("data", list);
    }


    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params) {
        log.info("params-----" + params);
        PageUtils page = licenceService.queryPage(params);
        return R.ok().put("data", page);
    }



}
